package com.dahua.messagesyncsh.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.dahua.messagesyncsh.common.Constants;
import com.dahua.messagesyncsh.config.redis.RedisUtil;
import com.dahua.messagesyncsh.criteria.LatestEquipmentStatusQueryCriteria;
import com.dahua.messagesyncsh.entity.LatestEquipmentStatus;
import com.dahua.messagesyncsh.mapper.LatestEquipmentStatusMapper;
import com.dahua.messagesyncsh.service.LatestEquipmentStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class LatestEquipmentStatusServiceImpl implements LatestEquipmentStatusService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private LatestEquipmentStatusMapper latestEquipmentStatusMapper;

    @Override
    public List<LatestEquipmentStatus> enquire(LatestEquipmentStatusQueryCriteria criteria) {
        if (criteria.getId() == null) {
            return latestEquipmentStatusMapper.enquire(criteria);
        }

        List<LatestEquipmentStatus> result = new ArrayList<>();

        Object hash = redisUtil.getHash(Constants.LATEST_EQUIPMENT_STATUS, String.valueOf(criteria.getId()));
        if (hash != null) {
            result.add(JSON.parseObject(JSON.toJSONString(hash), LatestEquipmentStatus.class));
        } else {
            List<LatestEquipmentStatus> enquire = latestEquipmentStatusMapper.enquire(criteria);
            if (enquire.size() == 1) {
                LatestEquipmentStatus info = enquire.get(0);
                redisUtil.setHash(Constants.LATEST_EQUIPMENT_STATUS, String.valueOf(info.getId()), info);
                result.add(info);
            }
        }

        return result;
    }

    @Override
    public Boolean insert(LatestEquipmentStatus latestEquipmentStatus) {
        if (latestEquipmentStatus.getId() == null
                || latestEquipmentStatus.getStatus() == null
                || latestEquipmentStatus.getStationId() == null
                || latestEquipmentStatus.getTime() == null
                || latestEquipmentStatus.getEqId() == null
                || latestEquipmentStatus.getLineId() == null
                || latestEquipmentStatus.getDeptId() == null
        ) {
            return false;
        }
        try {
            latestEquipmentStatusMapper.insert(latestEquipmentStatus);
        } catch (Exception e) {
            log.error("【LatestEquipmentStatus】：插入失败{}", e.getMessage());
            return false;
        }
        try {
            redisUtil.setHash(Constants.LATEST_EQUIPMENT_STATUS, String.valueOf(latestEquipmentStatus.getId()), latestEquipmentStatus);
        } catch (Exception e) {
            log.error("【LatestEquipmentStatus】：插入时缓存更新失败{}", e.getMessage());
        }
        return true;
    }

    @Override
    public Boolean update(LatestEquipmentStatus latestEquipmentStatus) {
        if (latestEquipmentStatus.getId() == null && latestEquipmentStatus.getEqId() == null) {
            return false;
        }

        if (latestEquipmentStatus.getId() == null) {
            latestEquipmentStatus.setId(latestEquipmentStatus.getEqId());
        }
        try {
            latestEquipmentStatusMapper.update(latestEquipmentStatus);
        } catch (Exception e) {
            log.error("【LatestEquipmentStatus】：更新失败{}", e.getMessage());
            return false;
        }
        try {
            redisUtil.setHash(Constants.LATEST_EQUIPMENT_STATUS, String.valueOf(latestEquipmentStatus.getId()), latestEquipmentStatus);
        } catch (Exception e) {
            log.error("【LatestEquipmentStatus】：更新时缓存更新失败{}", e.getMessage());
        }
        return true;
    }
}
